package com.software.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.software.dao.UserDao;
import com.software.model.AdminUser;
import com.software.model.ModelCreate;
import com.software.service.UserService;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService{
	
	@Autowired 
	private UserDao userDao;
    
   @Override
   public int insertUser(AdminUser user) {
       // TODO Auto-generated method stub
       return userDao.save(user);
   }
   
   @Override
   public List<ModelCreate> queryModel(String tableName){
	   List<ModelCreate> list = userDao.queryCol(tableName);
	   StringBuffer sb = new StringBuffer();
	   StringBuffer sb_col = new StringBuffer();
		for (ModelCreate mol : list) {
			
			String[] ColAry = mol.getColumnName().split("_");
			String newCol = "";
			if (ColAry.length == 0) {
				
			} else if (ColAry.length == 1) {
				newCol = mol.getColumnName().toLowerCase();
				
			} else if (ColAry.length == 2) {
				newCol = ColAry[0].toLowerCase()+ ColAry[1].substring(0,1).toUpperCase()+ ColAry[1].substring(1).toLowerCase();
			} else if (ColAry.length == 3) {
				newCol = ColAry[0].toLowerCase()+ ColAry[1].substring(0,1).toUpperCase()+ ColAry[1].substring(1).toLowerCase() + ColAry[2].substring(0,1).toUpperCase()+ ColAry[2].substring(1).toLowerCase();
			}
			
			String type = mol.getDataType();
			
			String newType = "";
			
			if (type.indexOf("bigint") > -1) {
				newType = "Long";
			} else if (type.indexOf("varchar") > -1) {
				newType = "String";
			} else if (type.indexOf("int") > -1) {
				newType = "Integer";
				
			} else if (type.indexOf("timestamp") > -1) {
				newType = "Data";
			} else {
				newType = "String";
			}
			sb_col.append("private").append(" ").append(newType).append(" ").append(newCol).append(";\n\r");
		}
		
		String[] TabNameAry = list.get(0).getTableName().split("_");
		String TabName = "";
		if (TabNameAry.length == 0) {
		} else if (TabNameAry.length == 1) {
			TabName = TabNameAry[0].substring(0,1).toUpperCase()+ TabNameAry[0].substring(1).toLowerCase();
		} else if (TabNameAry.length == 2) {
			TabName = TabNameAry[0].substring(0,1).toUpperCase() + TabNameAry[0].substring(1).toLowerCase() + TabNameAry[1].substring(0,1).toUpperCase()+ TabNameAry[1].substring(1).toLowerCase();
		} else if (TabNameAry.length == 3) {
			TabName = TabNameAry[0].substring(0,1).toUpperCase() + TabNameAry[0].substring(1).toLowerCase() + TabNameAry[1].substring(0,1).toUpperCase()+ TabNameAry[1].substring(1).toLowerCase() + TabNameAry[2].substring(0,1).toUpperCase()+ TabNameAry[2].substring(1).toLowerCase();
		}
		
		sb.append("public class ").append(TabName).append(" implements Serializable {").append("\n");
		sb.append(sb_col);
		sb.append("}");
		System.out.println(sb);
		
	     return list;
   }
   
   
   @Override
   public void buildSQLSearch(String tableName){
	   List<ModelCreate> list = userDao.queryCol(tableName);
	   StringBuffer sb = new StringBuffer();
	   StringBuffer sb_col = new StringBuffer();
	   int i = 0;
		for (ModelCreate mol : list) {
			
			String[] ColAry = mol.getColumnName().split("_");
			String newCol = "";
			if (ColAry.length == 0) {
				
			} else if (ColAry.length == 1) {
				newCol = mol.getColumnName().toLowerCase();
				
			} else if (ColAry.length == 2) {
				newCol = ColAry[0].toLowerCase()+ ColAry[1].substring(0,1).toUpperCase()+ ColAry[1].substring(1).toLowerCase();
			} else if (ColAry.length == 3) {
				newCol = ColAry[0].toLowerCase()+ ColAry[1].substring(0,1).toUpperCase()+ ColAry[1].substring(1).toLowerCase() + ColAry[2].substring(0,1).toUpperCase()+ ColAry[2].substring(1).toLowerCase();
			}
			if (i != 0) {
				sb_col.append("    ,");
			} else {
				sb_col.append("    ");
			}
			sb_col.append(mol.getColumnName()).append(" AS ").append(newCol).append("\n");
			i++;
		}
		
		
		sb.append("SELECT ").append("\n");
		sb.append(sb_col).append("\n");
		sb.append("  FROM ").append(tableName).append("\n");
		
		System.out.println(sb);
		
   }
   
	
	public void buildSQLInsert(String tableName){
		   List<ModelCreate> list = userDao.queryCol(tableName);
		   StringBuffer sb = new StringBuffer();
		   StringBuffer sb_col = new StringBuffer();
		   StringBuffer sb_value = new StringBuffer();
		   int i = 0;
			for (ModelCreate mol : list) {
				
				String[] ColAry = mol.getColumnName().split("_");
				String newCol = "";
				if (ColAry.length == 0) {
					
				} else if (ColAry.length == 1) {
					newCol = mol.getColumnName().toLowerCase();
					
				} else if (ColAry.length == 2) {
					newCol = ColAry[0].toLowerCase()+ ColAry[1].substring(0,1).toUpperCase()+ ColAry[1].substring(1).toLowerCase();
				} else if (ColAry.length == 3) {
					newCol = ColAry[0].toLowerCase()+ ColAry[1].substring(0,1).toUpperCase()+ ColAry[1].substring(1).toLowerCase() + ColAry[2].substring(0,1).toUpperCase()+ ColAry[2].substring(1).toLowerCase();
				}

				if (i != 0) {
					sb_col.append("    ,");
				} else {
					sb_col.append("    ");
				}
				sb_col.append(mol.getColumnName()).append(" \n");
				
				//  #{id},
				if (i != 0) {
					sb_value.append("    ,");
				} else {
					sb_value.append("    ");
				}
				sb_value.append("#{").append(newCol).append("}").append(" \n");

				i++;
			}
			
			
			sb.append("INSERT INTO ").append(tableName).append(" (\n");
			sb.append(sb_col).append("\n").append(")");
			sb.append("  VALUES (").append("\n");
			sb.append(sb_value).append("\n").append(")");
			System.out.println(sb);
			
	   }
	
	public void buildSQLUpdate(String tableName){
		
	}

}